package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.ejlchina.okhttps.HTTP;
import com.example.test.dialogfactory.MainActivity51.Data;
import com.example.test.dialogfactory.MainActivity51.Repo;
import com.example.test.dialogfactory.customerview.RecyclerPageBridge;
import com.google.gson.Gson;
import easier.recycler.BaseViewHolder;
import easier.recycler.TypeViewHolder;
import easier.safe.gson.GsonFactory;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity56 extends AppCompatActivity {

    private static final String url = "https://api.github.com/search/repositories?sort=stars&q=Android";
    private final HTTP http = HTTP.builder().build();
    private RecyclerView mRecycler;

    private Button mTest1;
    private Button mTest2;
    private Button mTest3;
    private Button mTest4;

    private int mode;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity56.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main56 );
        initView();
    }

    private void initView() {

        mRecycler = (RecyclerView) findViewById( R.id.recycler );

        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        Bridge bridge = new Bridge( mRecycler );
        //bridge.refresh();

        mTest1 = (Button) findViewById( R.id.test1 );
        mTest2 = (Button) findViewById( R.id.test2 );
        mTest3 = (Button) findViewById( R.id.test3 );
        mTest4 = (Button) findViewById( R.id.test4 );

        mTest1.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mode = 1;
                bridge.refresh();
            }
        } );

        mTest2.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mode = 2;
                bridge.refresh();
            }
        } );

        mTest3.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

               /* mode = 3;
                bridge.refreshCurrentPage();*/
            }
        } );

        mTest4.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mode = 4;
                bridge.refresh();
            }
        } );
    }

    private class Bridge extends RecyclerPageBridge< Data > {

        public Bridge( RecyclerView recyclerView ) {

            super( recyclerView );
        }

        @Override
        protected Observable< List< Data > > requestPageData( RecyclerView recyclerView, int page, int pageSize ) {

            String a = url;
            if ( mode == 1 ) {
                a = url + "XXXXSSSSSSSSXXXXXXXXXXXXSSSSSSSSSS";
            }

            final String b = a;

            return Observable.just( b )
                .map( new Function< String, List< Data > >() {
                    @Override
                    public List< Data > apply( String s ) throws Throwable {

                        if ( mode == 2 ) {
                            throw new IllegalStateException( "create IllegalStateException" );
                        }

                        if ( mode == 3 ) {
                            List< Data > all = getAll();
                            Collections.shuffle( all );
                            if ( page == 1 ) {
                                List< Data > result = new ArrayList<>();
                                for ( int i = 0; i < pageSize; i++ ) {
                                    result.add( all.get( i ) );
                                }
                                return result;
                            } else {
                                return new ArrayList<>();
                            }
                        }

                        Reader inputStream = http.sync( b ).addUrlPara( "page", page ).addUrlPara( "per_page", pageSize ).get()
                            .getBody().toCharStream();
                        Gson singletonGson = GsonFactory.getSingletonGson();
                        Repo repo = singletonGson.fromJson( inputStream, Repo.class );
                        List< Data > items = repo.items;

                        return items;
                    }
                } )
                .subscribeOn( Schedulers.io() )
                .observeOn( AndroidSchedulers.mainThread() );
        }

        @Override
        protected TypeViewHolder buildViewHolder( RecyclerView recyclerView, Data item, int position ) {

            return new BaseViewHolder( R.layout.activity_main51_item ) {
                @Override
                protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                    super.onBind( viewHolder, root, position, type, payloads );

                    TextView mNameText;
                    TextView mDescriptionText;
                    TextView mStarCountText;

                    mNameText = (TextView) root.findViewById( R.id.name_text );
                    mDescriptionText = (TextView) root.findViewById( R.id.description_text );
                    mStarCountText = (TextView) root.findViewById( R.id.star_count_text );

                    mNameText.setText( item.name );
                    mDescriptionText.setText( item.description );
                    mStarCountText.setText( String.format( "%d", item.stargazers_count ) );
                }
            };
        }

        @Nullable
        @Override
        protected TypeViewHolder configExceptionView( Throwable e ) {

            return new BaseViewHolder( R.layout.common_net_error );
        }

        @Override
        protected int configPageSize() {

            return 5;
        }
    }
}